/**
 * (created at 2011-1-25)
 */
package qiushuo.mysqlparser.ast.fragment;

import java.sql.SQLSyntaxErrorException;

import qiushuo.mysqlparser.ast.ASTNode;
import qiushuo.mysqlparser.ast.expression.primary.ParamMarker;
import qiushuo.mysqlparser.generator.SQLType;

/**
 * @author <a href="mailto:QiuShuo1985@gmail.com">QIU Shuo</a>
 */
public class Limit implements ASTNode {
    /** when it is negative, to sql generated must ignore this number */
    private final int offset;
    private final int size;
    private final ParamMarker offsetP;
    private final ParamMarker sizeP;

    public Limit(String offset, String size) throws SQLSyntaxErrorException {
        try {
            this.offset = Integer.parseInt(offset);
            this.size = Integer.parseInt(size);
        } catch (Exception e) {
            throw new SQLSyntaxErrorException("cannot convert string to int for limit: offset="
                                              + offset
                                              + ", size="
                                              + size, e);
        }
        this.offsetP = null;
        this.sizeP = null;
    }

    public Limit(ParamMarker offset, ParamMarker size) {
        this.offsetP = offset;
        this.sizeP = size;
        this.offset = -1;
        this.size = -1;
    }

    public Limit(ParamMarker offset, String size) throws SQLSyntaxErrorException {
        try {
            this.size = Integer.parseInt(size);
        } catch (Exception e) {
            throw new SQLSyntaxErrorException("cannot convert string to int for limit: offset="
                                              + offset
                                              + ", size="
                                              + size, e);
        }
        this.offset = -1;
        this.offsetP = offset;
        this.sizeP = null;
    }

    public Limit(String offset, ParamMarker size) throws SQLSyntaxErrorException {
        try {
            this.offset = Integer.parseInt(offset);
        } catch (Exception e) {
            throw new SQLSyntaxErrorException("cannot convert string to int for limit: offset="
                                              + offset
                                              + ", size="
                                              + size, e);
        }
        this.size = -1;
        this.offsetP = null;
        this.sizeP = size;
    }

    @Override
    public String generateSql(SQLType type) {
        // QS_TODO gen sql
        throw new UnsupportedOperationException("not implemented yet");
    }
}
